package net.pws.common.persistence.hibernate3;

import java.io.Serializable;
import java.util.List;

import net.pws.common.persistence.Finder;
import net.pws.common.persistence.IPage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


/**
 */
@Transactional(readOnly = true)
public abstract class HibernateFinder extends DaoSupportExtension implements
                                                                          Finder {
    
    protected Log logger = LogFactory.getLog(getClass());
    
    /**
     * @@org.springframework.transaction.interceptor.DefaultTransactionAttribute(propagationBehaviorName="PROPAGATION_REQUIRED",readOnly=true)
     */
    
    public Object loadById(Serializable id) {
        return (Object) getHibernateTemplate().load(getEntityClass(), id);
    }
    
    /**
     * @@org.springframework.transaction.interceptor.DefaultTransactionAttribute(propagationBehaviorName="PROPAGATION_REQUIRED",readOnly=true)
     */
    
    public Object findById(Serializable id) {
        return (Object) getHibernateTemplate().get(getEntityClass(), id);
    }
    
    /**
     * @@org.springframework.transaction.interceptor.DefaultTransactionAttribute(propagationBehaviorName="PROPAGATION_REQUIRED",readOnly=true)
     */
    
    public List findAll() {
        return getHibernateTemplate().loadAll(getEntityClass());
    }
    
    /**
     * @@org.springframework.transaction.interceptor.DefaultTransactionAttribute(propagationBehaviorName="PROPAGATION_REQUIRED",readOnly=true)
     */
    
    public IPage findAll(final int startPageNo, final int maxResultsInPage) {
        Assert.isTrue(startPageNo >= 0, "startPageNo should start from 0");
        return getHibernateTemplate().queryByPagination(getEntityClass(),
                                                        startPageNo,
                                                        maxResultsInPage);
    }
    
    /**
     * ���ظ�DaoҪ������Entity��Class,������Ҫʵ�ָ÷���
     */
    protected abstract Class getEntityClass();
    
}
